
def jwt_response_payload_handler(token, user=None, request=None):

    return {
        'token':token,
        'user_id':user.id,
        'username':user.username
    }


# 手机号登陆
from django.contrib.auth.backends import ModelBackend
import re
from users.models import User

def get_user_by_account(username):

    try:
        if re.match(r'1[3-9]\d{9}',username):
            user = User.objects.get(mobile=username)

        else:
            user = User.objects.get(username=username)
    except User.DoesNotExist:
        user = None

    return user


class LoginUsernameMobileModelBackend(ModelBackend):

    def authenticate(self, request, username=None, password=None,**kwargs):

        user = get_user_by_account(username=username)

        if user is not None and user.check_password(password):
            return user

        return None
